package com.richapp.launcher.database.system.dao;

import android.content.Context;
import android.util.Log;

import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.UpdateBuilder;
import com.richapp.launcher.database.system.SystemBaseDao;
import com.richapp.launcher.database.system.mode.DeviceModel;

import java.sql.SQLException;
import java.util.List;

public class DeviceModelDao extends SystemBaseDao<DeviceModel>{

	public DeviceModelDao(Context context) {
		super(context,DeviceModel.class);
	}

	public DeviceModel getByCpuIdDeviceIdDeviceNumber(String boxCpuId,String deviceId,String deviceNumber,String way){
        if(way == null){
            way = "00";
        }
		DeviceModel dm = null;
		try {
			dm = gettOp().queryBuilder().where().eq("boxCpuId", boxCpuId).and().eq("deviceId", deviceId).and().eq("deviceNumber", deviceNumber).and().eq("way",way).queryForFirst();
		} catch (SQLException e) {
			Log.e(getClass().getName(), e.getMessage());
		}
		return dm;
	}

	public List<DeviceModel> getByCpuIdDeviceIdDeviceNumber(String boxCpuId,String deviceId,String deviceNumber){
		List<DeviceModel> dmList = null;
		try {
			dmList = gettOp().queryBuilder().where().eq("boxCpuId", boxCpuId).and().eq("deviceId", deviceId).and().eq("deviceNumber", deviceNumber).query();
		} catch (SQLException e) {
			Log.e(getClass().getName(), e.getMessage());
		}
		return dmList;
	}
	
	public List<DeviceModel> getDeviceList(){
		List<DeviceModel> list = null;
		try {
			list = gettOp().queryBuilder().query();
		} catch (Exception e) {
			Log.e(getClass().getName(), e.getMessage());
		}
		return list;
	}
	
	public List<DeviceModel> getDeviceListByRoomName(String roomName){
		List<DeviceModel> list = null;
		try {
			list = gettOp().queryBuilder().where().eq("roomName", roomName).query();
		} catch (Exception e) {
			Log.e(getClass().getName(), e.getMessage());
		}
		return list;
	}
	
	public List<DeviceModel> getDeviceListByBoxCpuId(String boxCpuId){
		List<DeviceModel> list = null;
		try {
			list = gettOp().queryBuilder().where().eq("boxCpuId", boxCpuId).query();
		} catch (Exception e) {
			Log.e(getClass().getName(), e.getMessage());
		}
		return list;
	}
	
	public List<DeviceModel> getDeviceListByBoxCpuIdDeviceId(String boxCpuId,String deviceId,String way){
		List<DeviceModel> list = null;
		try {
            QueryBuilder queryBuilder =  gettOp().queryBuilder();
            queryBuilder.where().eq("boxCpuId", boxCpuId).and().eq("deviceId", deviceId).and().eq("way", way);
            list = queryBuilder.query();
		} catch (Exception e) {
			Log.e(getClass().getName(), e.getMessage());
		}
		return list;
	}
	
	public void deleteByBoxCpuId(String boxCpuId){
		try {
			DeleteBuilder<DeviceModel,Integer> dbl = gettOp().deleteBuilder();
			dbl.where().eq("boxCpuId", boxCpuId).or().isNull("boxCpuId");
			dbl.delete();
		} catch (SQLException e) {
			Log.e(getClass().getName(), e.getMessage());
		}
	}

	/**
	 *
	 * @param deviceId 设备id
	 */
	public List<DeviceModel>  getDeviceListByDeviceId
	(String deviceId){
		List<DeviceModel> list = null;
		try {
			QueryBuilder builder  = gettOp().queryBuilder();
			builder.where().eq("deviceId",deviceId);
			list = builder.query();

		}catch (Exception e){
			if (e!= null) {
				Log.e(getClass().getName(), e.getMessage());
			}

		}
		return list;

	}
	
	public void delete(String boxCpuId,String deviceId,String deviceNumber,String way){
		try {
			DeleteBuilder<DeviceModel,Integer> db = gettOp().deleteBuilder();
			db.where().eq("boxCpuId", boxCpuId).eq("deviceId", deviceId).and().eq("deviceNumber", deviceNumber).and().eq("way",way);
			db.delete();
		} catch (SQLException e) {
			Log.e(getClass().getName(), e.getMessage());
		}
	}

    public void  setAllOpen(String boxCpuId){
        try {
            UpdateBuilder<DeviceModel,Integer> db = gettOp().updateBuilder();
            db.where().eq("boxCpuId",boxCpuId).and().in("deviceId","01","02","03","10","11","19","1A","1B","1C","1D","1E","1F");
            db.updateColumnValue("deviceStatus","FF");
            db.update();
        } catch (SQLException e) {
			Log.e(getClass().getName(), e.getMessage());
        }
    }

    public void setAllClose(String boxCpuId){
        try {
            UpdateBuilder<DeviceModel,Integer> db = gettOp().updateBuilder();
            db.where().eq("boxCpuId",boxCpuId).and().in("deviceId","01","02","03","10","11","19","1A","1B","1C","1D","1E","1F");
            db.updateColumnValue("deviceStatus","00");
            db.update();
        } catch (SQLException e) {
			Log.e(getClass().getName(), e.getMessage());
        }
    }

    public List<DeviceModel> getOpenCloseAllResult(String boxCpuId){
        List<DeviceModel> list = null;
        try {
            list = gettOp().queryBuilder().where().eq("boxCpuId",boxCpuId).and().in("deviceId","01","02","03","10","11","19").query();
        } catch (Exception e) {
			Log.e(getClass().getName(), e.getMessage());
        }
        return list;
    }

	public List<DeviceModel> getByCpuIdDeviceIdDeviceNumberWay(String boxCpuId,String deviceId,String deviceNumber,String way){
		List<DeviceModel> dmList = null;
		try {
			dmList = gettOp().queryBuilder().where().eq("boxCpuId", boxCpuId).and().eq("deviceId", deviceId).and().eq("deviceNumber", deviceNumber).and().eq("way", way).query();
		} catch (SQLException e) {
			Log.e(getClass().getName(), e.getMessage());
		}
		return dmList;
	}


	public void delByDeviceIdNumber(String boxCpuId,String deviceId,String deviceNumber){
		try {
			DeleteBuilder<DeviceModel,Integer> db = gettOp().deleteBuilder();
			db.where().eq("boxCpuId", boxCpuId).and().eq("deviceId", deviceId).and().eq("deviceNumber", deviceNumber);
			db.delete();
		} catch (SQLException e) {
			Log.e(getClass().getName(), e.getMessage());
		}
	}

}
